package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.dto.yx7881.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 淘号号 属性匹配
 */
@Slf4j
@Service
public class PAOYANGAttr {

    public  List<GamesRes> getGames(SalePartner salePartner) {
        Map<String, String> header = new TreeMap<>();
        header.put("appkey", salePartner.getSaleKey());
        header.put("appsecret", salePartner.getOther());
        String result = HttpUtil.doGet("https://sys.api.paoyang.com/openapi/category/list", null, header);
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            gamesRes.setId(jsonObject.getString("id"));
            gamesRes.setGameName(jsonObject.getString("category_name"));
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    public  List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, String> header = new TreeMap<>();
        header.put("appkey", "baixing");
        header.put("appsecret", "9BEEFE37EDF78027AC1FE6DD832FE993");
        String result = HttpUtil.doGet("https://sys.api.paoyang.com/openapi/attr/list/" + gameId, null, header);
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<InterServerRes> interServerRes = new ArrayList<>();
        List<InterServerRes> list = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjects) {
            if(jsonObject.getString("attr_name").equals("按系统")||jsonObject.getString("attr_name").equals("按大区")
                    ||jsonObject.getString("attr_name").equals("按服务器")||jsonObject.getString("attr_name").equals("按区服")){
                list = new ArrayList<>();
                InterServerRes res = new InterServerRes();
                res.setId(jsonObject.getString("attr_value"));
                res.setName(jsonObject.getString("attr_name"));
                List<JSONObject> serJSON = JSONObject.parseArray(jsonObject.getString("_child"), JSONObject.class);
                for(JSONObject jsonObject1:serJSON){
                    InterServerRes res1 = new InterServerRes();
                    res1.setId(jsonObject1.getString("attr_value"));
                    res1.setName(jsonObject1.getString("attr_name"));
                    list.add(res1);
                }
                res.setInterServerRes(list);
                interServerRes.add(res);
            }
        }
        return interServerRes;
    }

    public  List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, String> header = new TreeMap<>();
        header.put("appkey", "baixing");
        header.put("appsecret", "9BEEFE37EDF78027AC1FE6DD832FE993");
        String result = HttpUtil.doGet("https://sys.api.paoyang.com/openapi/attr/list/" + gameId, null, header);


        String result2 = HttpUtil.doGet("https://sys.api.paoyang.com/openapi/form/list/" + gameId, null, header);

        JSONObject resJson = JSONObject.parseObject(result);
        JSONObject resJson2 = JSONObject.parseObject(result2);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<JSONObject> jsonObjects2 = JSONObject.parseArray(resJson2.getString("data"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        List<JSONObject> collect2 = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = jsonObjects2.stream().filter(d -> d.getString("form_type").equals("2")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = jsonObjects2.stream().filter(d -> d.getString("form_type").equals("3")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = jsonObjects2.stream().filter(d -> d.getString("form_type").equals("4")).collect(Collectors.toList());
        }
        for(JSONObject coll:collect){
            for(JSONObject jsonObject : jsonObjects){
                if(coll.getString("form_name").equals(jsonObject.getString("attr_name"))){
                    collect2.add(jsonObject);
                }
            }
        }

        List<InterAttrRes> interAttrResList = new ArrayList<>();
        for (JSONObject object : collect2) {
            String string = object.getString("_child");
            JSONArray jsonArray = JSONArray.parseArray(string);
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setId(object.getString("attr_value"));
            interAttrRes.setName(object.getString("attr_name"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            for(Object o : jsonArray) {
                InterAttrRes interAttrRes1 = new InterAttrRes();
                interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("attr_value"));
                interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("attr_name"));
                interAttrResList1.add(interAttrRes1);
                interAttrRes.setInterServerRes(interAttrResList1);
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }

}